package com.itkjb.leisurely.resource.sys.controller;


import com.baomidou.mybatisplus.core.metadata.IPage;
import com.itkjb.leisurely.core.base.BaseConstants;
import com.itkjb.leisurely.core.base.BaseDto;
import com.itkjb.leisurely.core.base.BaseResult;
import com.itkjb.leisurely.core.base.controller.BaseController;
import com.itkjb.leisurely.resource.sys.model.SysDepartmentModel;
import com.itkjb.leisurely.resource.sys.service.ISysDepartmentService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import java.io.Serializable;
import java.util.List;
import java.util.Set;

/**
 * <p>
 * 组织结构 接口层
 * </p>
 *
 * @author itkjb.com
 * @since 2020-06-30
 */
@RestController
@RequestMapping("/sys/sys-department")
@Api(value = "组织结构", tags = {"组织结构相关接口"})
@Slf4j
public class SysDepartmentController extends BaseController<SysDepartmentModel, ISysDepartmentService> {


    /**
     * 获取组织结构列表
     *
     * @param param    查询通用参数
     * @param model    特定条件参数
     * @param request
     * @param response
     * @return
     */
    @GetMapping
    @ApiOperation(value = "获取组织结构列表", notes = "支持分页参数")
    @PreAuthorize("hasAnyAuthority('SysDep','SysDep:query') || hasRole('"+ BaseConstants.ADMINISTRATOR_ROLE_CODE +"') ")
    public BaseResult<IPage<SysDepartmentModel>> page(BaseDto param, SysDepartmentModel model, HttpServletRequest request, HttpServletResponse response) {
        BaseResult<IPage<SysDepartmentModel>> res = new BaseResult<>();
        if(StringUtils.isBlank(model.getParentId())){
            model.setParentId(BaseConstants.DEFAULT_TOP_PARENT_ID);
        }
        res = super.page(param, model);
        return res;
    }

    /**
     * 获取组织结构列表
     *
     * @param param    查询通用参数，分页参数不可用
     * @param model    特定条件参数
     * @param request
     * @param response
     * @return
     */
    @GetMapping("/list")
    @ApiOperation(value = "获取组织结构列表", notes = "不支持分页")
    @PreAuthorize("hasAnyAuthority('SysDep','SysDep:query') || hasRole('"+ BaseConstants.ADMINISTRATOR_ROLE_CODE +"') ")
    public BaseResult<List<SysDepartmentModel>> list(BaseDto param, SysDepartmentModel model, HttpServletRequest request, HttpServletResponse response) {
        BaseResult<List<SysDepartmentModel>> res = new BaseResult<>();
        if(StringUtils.isBlank(model.getParentId()) && StringUtils.isBlank(model.getId())){
            // 在id 和 parentId 都没有指定的情况下，将parendId置位顶级id
            model.setParentId(BaseConstants.DEFAULT_TOP_PARENT_ID);
        }
        return super.list(param, model);

    }

    /**
     * 获取组织结构列表
     *
     * @param param    查询通用参数，分页参数不可用
     * @param model    特定条件参数
     * @param request
     * @param response
     * @return
     */
    @GetMapping("/lazyList")
    @ApiOperation(value = "获取组织结构列表", notes = "不支持分页")
    @PreAuthorize("hasAnyAuthority('SysDep','SysDep:query') || hasRole('"+ BaseConstants.ADMINISTRATOR_ROLE_CODE +"') ")
    public BaseResult<List<SysDepartmentModel>> lazyList(BaseDto param, SysDepartmentModel model, HttpServletRequest request, HttpServletResponse response) {
        BaseResult<List<SysDepartmentModel>> res = new BaseResult<>();
        if(StringUtils.isBlank(model.getParentId())){
            model.setParentId(BaseConstants.DEFAULT_TOP_PARENT_ID);
        }
        return super.list(param, model);

    }

    /**
     * 新增组织结构
     *
     * @param param
     * @param request
     * @param response
     * @return
     */
    @PostMapping
    @ApiOperation(value = "新增组织结构", notes = "")
    @PreAuthorize("hasAnyAuthority('SysDep:add') || hasRole('"+ BaseConstants.ADMINISTRATOR_ROLE_CODE +"') ")
    public BaseResult<SysDepartmentModel> add(@Valid @RequestBody SysDepartmentModel param, HttpServletRequest request, HttpServletResponse response) {
        BaseResult<SysDepartmentModel> res = new BaseResult<>();
        res = super.add(param);
        return res;
    }

    /**
     * 获取组织结构详情
     *
     * @param id
     * @param response
     * @return
     */
    @GetMapping("/{id}")
    @ApiOperation(value = "获取组织结构详情", notes = "")
    @ApiParam(name = "id", type = "Serializable", required = true)
    @PreAuthorize("hasAnyAuthority('SysDep:info') || hasRole('"+ BaseConstants.ADMINISTRATOR_ROLE_CODE +"') ")
    public BaseResult<SysDepartmentModel> info(@PathVariable("id") Serializable id, HttpServletResponse response) {
        BaseResult<SysDepartmentModel> res = new BaseResult<>();
        res = super.info(id);
        return res;
    }

    /**
     * 修改组织结构
     *
     * @param param
     * @param request
     * @param response
     * @return
     */
    @PutMapping
    @ApiOperation(value = "修改组织结构", notes = "")
    @PreAuthorize("hasAnyAuthority('SysDep:edit') || hasRole('"+ BaseConstants.ADMINISTRATOR_ROLE_CODE +"') ")
    public BaseResult<SysDepartmentModel> edit(@RequestBody SysDepartmentModel param, HttpServletRequest request, HttpServletResponse response) {
        BaseResult<SysDepartmentModel> res = new BaseResult<>();
        if(StringUtils.isBlank(param.getId())){
            res.setMsg("参数缺失");
            return res;
        }else if(param.getId().equals(param.getParentId())){
            res.setMsg("不能指定自身为上级组织");
            return res;
        }else {
            Set<String> subIds = this.baseService.getSubIds(param.getId());
            if(subIds !=null && subIds.size()>0 && subIds.contains(param.getParentId())){
                res.setMsg("不能指定自身的下级为上级组织");
                return res;
            }
        }
        res = super.edit(param);
        return res;
    }

    /**
     * 删除组织结构
     *
     * @param ids
     * @param request
     * @param response
     * @return
     */
    @DeleteMapping
    @ApiOperation(value = "删除组织结构", notes = "注意参数是数字，就算单个，也要以数字的形式传递")
    @PreAuthorize("hasAnyAuthority('SysDep:del') || hasRole('"+ BaseConstants.ADMINISTRATOR_ROLE_CODE +"') ")
    public BaseResult<SysDepartmentModel> delete(@RequestBody String[] ids, HttpServletRequest request, HttpServletResponse response) {
        BaseResult<SysDepartmentModel> res = new BaseResult<>();
        res = super.delete(ids);
        return res;
    }

}

